<template>
  <el-dialog
    class="task-submit-dialog"
    title="任务提交"
    width="400px"
    :visible.sync="dialogVisible"
    :close-on-click-modal="true"
    v-dialogDrag="true"
    append-to-body
    @open="handleDialogOpen"
    @closed="handleDialogClosed"
  >
    <el-form ref="submitTaskFormRef" :model="formData" :rules="formRules" size="small" label-width="100px">
      <el-form-item label="编制人" prop="organizerUserId" label-width="80px">
        <el-select
          style="width: 100%;"
          v-model="formData.organizerUserId"
          placeholder="请选择"
          :clearable="true"
        >
          <el-option
            v-for="item in organizerUserList"
            :key="item.userId"
            :label="item.nickName"
            :value="item.userId"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="审核人" prop="checkerUserId" label-width="80px">
        <el-select
          style="width: 100%;"
          v-model="formData.checkerUserId"
          placeholder="请选择"
          :clearable="true"
        >
          <el-option
            v-for="item in checkerUserList"
            :key="item.userId"
            :label="item.nickName"
            :value="item.userId"
          ></el-option>
        </el-select>
      </el-form-item>
    </el-form>

    <div class="dialog-footer" slot="footer">
      <el-button type="primary" :loading="btnLoading" @click="handleConfirm">确 定</el-button>
      <el-button @click="dialogVisible = false">关 闭</el-button>
    </div>
  </el-dialog>
</template>

<script>
import { allocatedList } from '@/api/person/person'
import { initiateApproval } from '@/api/person/entrustTask'

export default {
  name: 'TaskSubmitDialog',
  components: {},
  dicts: [],
  props: {
    show: {
      type: Boolean,
      default: false,
    },
    taskDetectionEquipmentId: {
      type: String,
      default: ''
    }
  },
  data() {
    return {
      organizerUserList: [],
      checkerUserList: [],
      formData: {},
      formRules: {
        organizerUserId: [{ required: true, message: '请选择编制人', trigger: 'change' }],
        checkerUserId: [{ required: true, message: '请选择审核人', trigger: 'change' }]
      },
      btnLoading: false,
    }
  },
  computed: {
    dialogVisible: {
      get() {
        return this.show
      },
      set(value) {
        this.$emit('update:show', value)
      }
    }
  },
  created() {
    this.initFormData()
  },
  methods: {
    // 弹框打开时
    handleDialogOpen() {
      this.getOrganizerCheckerUserList()
    },
    // 弹框关闭时
    handleDialogClosed() {
      this.initFormData()
      this.$refs.submitTaskFormRef.resetFields()
    },
    // 初始化表单数据
    initFormData() {
      this.formData = {
        organizerUserId: '',
        checkerUserId: '',
      }
    },
    // 获取表单数据
    getFormData() {
      const data = JSON.parse(JSON.stringify(this.formData))
      data.id = this.taskDetectionEquipmentId
      return data
    },
    // 获取编制人和审核人列表
    getOrganizerCheckerUserList() {
      allocatedList({ roleId: '1854171586286514178', pageNum: 1, pageSize: 1000 }).then(res => {
        const rows = res.rows || []
        this.organizerUserList = rows
      }).catch(err => {})

      allocatedList({ roleId: '1854171360926560258', pageNum: 1, pageSize: 1000 }).then(res => {
        const rows = res.rows || []
        this.checkerUserList = rows
      }).catch(err => {})
    },
    // 点击确定
    handleConfirm() {
      this.$refs.submitTaskFormRef.validate(valid => {
        if (!valid) return
        const data = this.getFormData()
        console.log('表单数据', data)
        initiateApproval(data).then(res => {
          this.$message.success('操作成功')
          this.dialogVisible = false
          this.$emit('submit-task-success', res.data)
        }).catch(err => {})
      })
    }
  }
}
</script>

<style lang="scss" scoped>
.task-submit-dialog {

  ::v-deep .el-dialog__body {
    max-height: 70vh;
    overflow-y: auto;
  }

  ::v-deep .transfer {
    width: 100%;

    .el-transfer-panel {
      width: calc(calc(100% - 182px) / 2);
    }

    .el-transfer-panel__body {
      height: calc(70vh - 40px - 100px - 15px);
    }

    .el-transfer-panel__list {
      height: calc(100% - 32px - 15px);
    }
  }
}
</style>
